package org.jeecg.modules.music.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.base.service.BaseCommonService;
import org.jeecg.modules.music.entity.MusAlbumSong;
import org.jeecg.modules.music.entity.MusArtist;
import org.jeecg.modules.music.entity.MusListSong;
import org.jeecg.modules.music.entity.MusSong;
import org.jeecg.modules.music.model.MusSongListCategoryModel;
import org.jeecg.modules.music.service.IMusAlbumSongListService;
import org.jeecg.modules.music.service.IMusListSongService;
import org.jeecg.modules.system.entity.SysPosition;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.model.SysUserSysDepartModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags="歌单专辑")
@RestController
@RequestMapping("/mus/albumorsonglist")
public class MusAlbumSongListController {

    @Resource
    private BaseCommonService baseCommonService;

    @Autowired
    private IMusAlbumSongListService musAlbumSongListService;

    @Autowired
    private IMusListSongService musListSongService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<MusAlbumSong>  add(@RequestBody JSONObject jsonObject) {
        Result<MusAlbumSong> result = new Result<MusAlbumSong>();
        try {
            MusAlbumSong albumSong = JSON.parseObject(jsonObject.toJSONString(), MusAlbumSong.class);
            albumSong.setCreateTime(new Date());
            albumSong.setDelFlag(CommonConstant.DEL_FLAG_0);
            musAlbumSongListService.addAlbumSong(albumSong);
            result.success("添加成功！");
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @ApiOperation("更新歌单歌曲数目")
    @RequestMapping(value = "/updateTrackCount", method = {RequestMethod.GET})
    public Result updatePlayCount(@RequestParam(name = "id",required = true)String id){
        QueryWrapper<MusListSong> wq = new QueryWrapper<>();
        wq.eq("songlist_id",id);
        List<MusListSong> q = musListSongService.list(wq);
        MusAlbumSong album = musAlbumSongListService.getById(id);
        album.setTrackCount(q.size());
        musAlbumSongListService.updateById(album);
        return Result.ok();
    }
    @ApiOperation("编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<MusAlbumSong> edit(@RequestBody JSONObject jsonObject) {
        Result<MusAlbumSong> result = new Result<MusAlbumSong>();
        try {
            MusAlbumSong albumSong = musAlbumSongListService.getById(jsonObject.getString("id"));
            baseCommonService.addLog("编辑歌单，id： " +jsonObject.getString("id") ,CommonConstant.LOG_TYPE_2, 2);
            if(albumSong==null) {
                result.error500("未找到对应实体");
            }else {
                MusAlbumSong songList = JSON.parseObject(jsonObject.toJSONString(), MusAlbumSong.class);
                songList.setUpdateTime(new Date());
                musAlbumSongListService.editAlbumSong(songList);
                result.success("修改成功!");
            }
        }catch (Exception e){
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }
    @RequestMapping(value = "/addSongs", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<?> handleAddSongs(@RequestBody JSONObject jsonObject){

        this.musAlbumSongListService.saveSongs(jsonObject.getString("songlistId"),jsonObject.getString("songIds"));
        return Result.ok("批量添加歌曲成功");
    }
    /**
     * 根据 orgCode 查询用户，包括子部门下的用户
     * 针对通讯录模块做的接口，将多个部门的用户合并成一条记录，并转成对前端友好的格式
     */
    @AutoLog(value = "专辑-分页列表查询")
    @ApiOperation(value = "专辑-分页列表查询", notes = "专辑-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<MusAlbumSong>> queryPageList(MusAlbumSong albumSong,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        Result<IPage<MusAlbumSong>> result = new Result<IPage<MusAlbumSong>>();
        QueryWrapper<MusAlbumSong> queryWrapper = new QueryWrapper<MusAlbumSong>();

        queryWrapper.eq("list_type",2);
        if(oConvertUtils.isNotEmpty(albumSong.getListname())){
            queryWrapper.and(wrapper ->wrapper.like("listname",albumSong.getListname()).or().like("description",albumSong.getListname()));
        }
        Page<MusAlbumSong> page = new Page<MusAlbumSong>(pageNo, pageSize);
        IPage<MusAlbumSong> pageList = musAlbumSongListService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @ApiOperation("分页")
    @GetMapping("/queryByMusCodeForAlbumSongList")
    public Result<IPage<MusAlbumSong>> queryPageList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(name = "musCode",required = false) String musCode,
                                                     MusAlbumSong albumParams){
        Result<IPage<MusAlbumSong>> result = new Result<IPage<MusAlbumSong>>();
        Page<MusAlbumSong> page = new Page<MusAlbumSong>(pageNo, pageSize);
        IPage<MusAlbumSong> pageList = musAlbumSongListService.querySongListPage(page,musCode,albumParams);
        result.setSuccess(true);
        result.setResult(pageList);
        log.info(pageList.toString());
        return result;
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        baseCommonService.addLog("删除歌单，id： " +id ,CommonConstant.LOG_TYPE_2, 3);
        this.musAlbumSongListService.deleteAlbum(id);
        return Result.ok("删除成功");
    }

    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        baseCommonService.addLog("批量删除， ids： " +ids ,CommonConstant.LOG_TYPE_2, 3);
        this.musAlbumSongListService.deleteBatchAlbums(ids);
        return Result.ok("批量删除歌单成功");
    }



    @ApiOperation("详情")
    @GetMapping("/detail")
    public Result<IPage<MusSong>> queryPageSongs(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                @RequestParam(name = "songlistId",required = false) String songlistId,
                                                MusSong songParams){
        Result<IPage<MusSong>> result = new Result<IPage<MusSong>>();
        Page<MusSong> page = new Page<MusSong>(pageNo, pageSize);
        IPage<MusSong> pageList = musAlbumSongListService.querySongDetailPage(page,songlistId,songParams);
        result.setSuccess(true);
        result.setResult(pageList);
        log.info(pageList.toString());
        return result;
    }

    @ApiOperation("获取更多歌曲")
    @GetMapping("/querySongs")
    public Result<IPage<MusSong>> queryNoPageSongs(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 @RequestParam(name = "songlistId",required = false) String songlistId,
                                                 MusSong songParams){
        Result<IPage<MusSong>> result = new Result<IPage<MusSong>>();
        Page<MusSong> page = new Page<MusSong>(pageNo, pageSize);
        IPage<MusSong> pageList = musAlbumSongListService.queryNoSongDetailPage(page,songlistId,songParams);
        result.setSuccess(true);
        result.setResult(pageList);
        log.info(pageList.toString());
        return result;
    }

    @RequestMapping(value = "/deteleRelation", method = RequestMethod.DELETE)
    public Result<?> handleDeleteListSong(@RequestParam(name="songlistId",required=true) String songlistId,@RequestParam(name="songId",required=true)String songId){
        this.musAlbumSongListService.deleteListSong(songlistId,songId);
        return Result.ok("批量删除成功");
    }

    @RequestMapping(value = "/deleteBatchRelation", method = RequestMethod.DELETE)
    public Result<?> handleDeleteListSongs(@RequestParam(name="songlistId",required=true) String songlistId,@RequestParam(name="songIds",required=true)String songIds){
        this.musAlbumSongListService.deleteListSongBatch(songlistId,songIds);
        return Result.ok("批量删除成功");
    }

}
